public class Solution147 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val,ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode insertionSortList(ListNode head) {
        if (head==null){
            return null;
        }
        ListNode f=new ListNode(Integer.MIN_VALUE,null);
        ListNode first=head;
        while (first!=null){
            ListNode cur=f;
            ListNode last=cur;
            while (cur!=null && first.val>cur.val){
                last=cur;
                cur=cur.next;
            }
            last.next=first;
            ListNode temp=first.next;
            first.next=cur;
            first=temp;
        }
        return f.next;
    }

    public static void main(String[] args) {
        System.out.println(new Solution147().insertionSortList(new ListNode(-1,new ListNode(5,new ListNode(3,new ListNode(4,new ListNode(0)))))).next.val);
    }
}
